//
//  SFPNutritionalRecordController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit
import BRPickerView
import ProgressHUD

class SFPNutritionalRecordController: SFPBaseViewController {

    @IBOutlet var dateTF: UITextField!
    @IBOutlet var intakeTF: UITextField!
    @IBOutlet var foodTF: UITextField!
    @IBOutlet var typeTF: UITextField!
    var selectIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func gotoBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    @IBAction func gotoSave(_ sender: Any) {
        self.view.endEditing(true)
        if self.dateTF.text!.isEmpty {
            showTips("Please select record date")
            return
        }
        if self.foodTF.text!.isEmpty {
            showTips("Please enter ingest food")
            return
        }
        let model = SFPNutrientIntakeModel()
        model.date = self.dateTF.text!
        model.name = self.foodTF.text!
        model.type = self.selectIndex
        model.intake = Int(self.intakeTF.text!)!
        model.isDefault = false
        model.saveToDB()
        let helper = SFPNutrientIntakeModel.getUsingLKDBHelper()
        helper.delete(with: SFPNutrientIntakeModel.self, where: "isDefault = true")
        ProgressHUD.showSuccess("More record addition succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    @IBAction func changeToLastType(_ sender: Any) {
        if selectIndex > 0 {
            selectIndex -= 1
        }
        self.typeTF.text = EAT_TYPES[selectIndex]
    }
    @IBAction func changeToNextType(_ sender: Any) {
        if selectIndex < 2 {
            selectIndex += 1
        }
        self.typeTF.text = EAT_TYPES[selectIndex]
    }
    @IBAction func gotoSelectRecordDate(_ sender: Any) {
        self.view.endEditing(true)
        let pickerView = BRDatePickerView(pickerMode: .YMD)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.resultBlock = { selectDate,selectValue in
            self.dateTF.text = selectDate?.toString(format: "yyyy.MM.dd")
        }
        pickerView.show()
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
